package com.javasee.data;


import com.javasee.common.dbsharding.DbShardingContextHolder;
import com.javasee.data.mapper.TbOrdersMapper;
import com.javasee.data.mapper.sqlite.TbTestsMapper;
import com.javasee.data.mapper.sqlite.UsersMapper;
import com.javasee.data.model.TbOrders;
import com.javasee.data.model.sqlite.TbTests;
import com.javasee.data.model.sqlite.Users;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Project Name:
 * File Name   :
 * Package Name:
 *
 * @author : xinyanfei
 * @date :
 * Copyright (c) , 京东商城  All Rights Reserved.
 */
@Slf4j
@RestController
public class DbController {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private TbTestsMapper tbTestsMapper;

    @Autowired
    TbOrdersMapper tbOrdersMapper;

    @ResponseBody
    @GetMapping("/db/users")
    public Result getUsersMapper() {
        DbShardingContextHolder.sharding("tb_users", "zhangsan");
        Users users = new Users();
        users.setId(1);
        users.setPin("zhangsan");

        Users data = usersMapper.findUserById(users);
        System.out.println("usersMapper.findAll() = " + data);

        return Result.builder().build(data).setState(Result.success);
    }

    @ResponseBody
    @GetMapping("/db/add/tests")
    public Result addTests() {

        //单库单表，application.yml配置或不配置，都可以,在调用入口DbShardingContextHolder.sharding可以不设定。 DbShardingRouterFactory ，DbShardingMyBatisInterceptor类进行了修改，新增了类SingleRouter
        DbShardingContextHolder.sharding("tb_tests", "apple");
        TbTests tests = new TbTests();
        tests.setId(1);
        Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tests.setCreateTime( formatter.format(new Date()));
        tests.setPrice(1000);
        tests.setProductName("apple");

        int ok = tbTestsMapper.insert(tests);
        System.out.println("ok = " + ok);
        return Result.builder().build(ok).setState(Result.success);
    }

    @ResponseBody
    @GetMapping("/db/add/tests/single")
    public Result addTests1() {
        //单库单表，application.yml配置或不配置，都可以,在调用入口DbShardingContextHolder.sharding可以不设定。 DbShardingRouterFactory ，DbShardingMyBatisInterceptor类进行了修改，新增了类SingleRouter

//        DbShardingContextHolder.single("tb_tests");
        TbTests tests = new TbTests();
        tests.setId(1);
        Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tests.setCreateTime( formatter.format(new Date()));
        tests.setPrice(1000);
        tests.setProductName("watch");

        int ok = tbTestsMapper.insert(tests);
        System.out.println("ok = " + ok);
        return Result.builder().build(ok).setState(Result.success);
    }



    @ResponseBody
    @GetMapping("/db/add/TbOrders/assign")
    public Result addTbOrders() {
        //指定库，指定表索引
        DbShardingContextHolder.assign("tb_orders",2,0);
        TbOrders tests = new TbOrders();
//        Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tests.setCreateTime( new Date());
        tests.setOrderid(10001);
        int ok = tbOrdersMapper.insert(tests);
        System.out.println("ok = " + ok);
        return Result.builder().build(ok).setState(Result.success);
    }
}

@Data
class  Result{

    private static Result result=new Result();

    private String state;

    private Object data;

    static final String success="success";
    static final String fail="fail";

    Result(){
        this.state=Result.success;
        this.data=null;
    }

    static Result builder(){
        return result;
    }

    public Result setState(String state) {
        this.state = state;
        return this;
    }

    Result build(Object data){
        this.data=data;
        return this;
    }
}